/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Vue.Design;

import Vue.Fenetre;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.BorderFactory;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 * Amelioration visuelle de la fenetre.
 * Cette classe a ete codee rapidement sans trop etre optimisee. Toutes les fonctionnalites
 * ici presentes sont optionnelles, non indispensables au bon fonctionnement de la 
 * fenetre.
 * @author Jo
 */
public class DesignFenetre {
    
    /**-----------DESIGN-----------**/
    static Color bordureColor = new Color(225,225,205, 0);
    static Color bordureIconeColor = new Color(235,235,235);
    static Color beigeClair = new Color(255 , 250, 210);
    
    
    
    static final int rgbFenetre = 235;//230 bleuClair
    static final Color fenetreBgColor = new Color(rgbFenetre, rgbFenetre, rgbFenetre+20);
    static final int plus = 20;
    static final int rgbListesColor = 218;
    public static final Color listesColor = new Color(rgbListesColor,rgbListesColor,rgbListesColor+20);
    
    static Color listesColorOver = /*new Color(listesColor.getRed()+plus/2,
                                            listesColor.getGreen()+plus/2,
                                            255);*///215,215,255
                                    fenetreBgColor;
    static Color listesColorSelected = /*new Color(listesColor.getRed()+plus,
                                                listesColor.getGreen()+plus,
                                                255);*///150,150,255
                                        fenetreBgColor;
    
    
    static final int rgbFGColor = 100;
    static public Color FGColor = new Color(rgbFGColor, rgbFGColor, rgbFGColor);
    static public Color labelIntituleColor = new Color(150,150,150);
    public static final Color FGColorSelected = new Color(FGColor.getRed(), FGColor.getGreen(), FGColor.getBlue()+90);
    
    public static void designer(final Fenetre fenetre){
        
        fenetre.getContentPane().setBackground(fenetreBgColor);
        fenetre.panneauPrincipal.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        
        fenetre.jScrollPane_droite.setViewportBorder(BorderFactory.createEmptyBorder());
        fenetre.jScrollPane_droite.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
        
        fenetre.jList_groupes.setBackground(listesColor);
        fenetre.jList_contacts.setBackground(listesColor);
        
        designerListe(fenetre.jList_groupes, fenetre);
        designerListe(fenetre.jList_contacts, fenetre);
        
        setBordure(fenetre.jScrollPane_groupes);
        setBordure(fenetre.jScrollPane_contacts);
        
        setBordureIcone(fenetre.panelDetails.labelIcone);
        setBordureIcone(fenetre.panelEdition.labelIcone);
        
        fenetre.jList_groupes.addListSelectionListener(new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent e) {
                fenetre.repaint();
            }
        });
        //essai pour jsp
        /*//tester en redefinissant JViewPort
        //fenetre.jList_contacts.setBackground(new Color(255, 255, 230,0));
        //fenetre.jList_contacts.setOpaque(false);
        JPanel ppp = new JPanel();
        ppp.setBackground(Color.blue);
        ppp.setPreferredSize(new Dimension(10, 10));
        ppp.add(new JLabel("a"));
        fenetre.jList_contacts.setPreferredSize(new Dimension(2000, 2000));
        fenetre.jScrollPane_contacts.setCorner(JScrollPane.LOWER_RIGHT_CORNER, ppp);
        fenetre.jScrollPane_contacts.setVerticalScrollBar(new JScrollBar(JScrollBar.VERTICAL, 1, 0, 1, 2));
        fenetre.jScrollPane_contacts.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);*/
    }
    
    
    public static void designerListe(final JList jList, final Fenetre f){
        jList.setCellRenderer(new DesignFenetre.CustomCellOver(-10));
        jList.addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseMoved(MouseEvent e) {
                int i = jList.locationToIndex(e.getPoint());
                jList.setCellRenderer(new DesignFenetre.CustomCellOver(i));
                f.repaint();
            }
        });
        jList.addMouseListener(new MouseAdapter() {
            
            @Override
            public void mouseEntered(MouseEvent e) {
                int i = jList.locationToIndex(e.getPoint());
                jList.setCellRenderer(new DesignFenetre.CustomCellOver(i));
                f.repaint();
            }

            @Override
            public void mouseExited(MouseEvent e) {
                jList.setCellRenderer(new DesignFenetre.CustomCellOver(-10));
                f.repaint();
            }
        });
        
        for (final Component c : jList.getComponents()) {
            c.setBackground(Color.red);
            c.addMouseListener(new MouseAdapter() {

                @Override
                public void mouseEntered(MouseEvent e) {
                    c.setBackground(Color.red);
                }
                
            });
        }
    }
    
    public static void setBordure(JScrollPane js){
        int marge = 4;
        Border b1 = BorderFactory.createLineBorder(bordureColor);
        Border b2 = BorderFactory.createEmptyBorder(marge, marge, marge, 0);
        js.setBorder(BorderFactory.createCompoundBorder(b2, b1));
    }
    public static void setBordureIcone(JLabel labelIcone){
        Color gris = new Color(110,110,110, 120);
        labelIcone.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(gris)
                , BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(bordureIconeColor, 4), 
                BorderFactory.createLineBorder(gris))));
    }
    
    
    public static class CustomCellOver extends DefaultListCellRenderer{
        int iOver;

        public CustomCellOver(int i) {
            this.iOver = i;
        }
        
        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            
            int bordure = 4;
            setBorder(new EmptyBorder(bordure, 6, bordure, bordure));
            setForeground(FGColor);
            
            if(!isSelected){
                if(iOver==index){
                    setBackground(listesColorOver);
                }
            }
            else{//si selectionned
                setBackground(listesColorSelected);
                setForeground(FGColorSelected);
                setFont(new Font("Arial", Font.BOLD, 11));
            }
            
            return this;
        }
    }
    /**-----------FIN DESIGN-----------**/
}
